package org.farm.product.domain.chain.handler;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import lombok.extern.slf4j.Slf4j;
import org.farm.product.common.enums.ProductSkuStatusEnum;
import org.farm.product.domain.bo.ProductSkuBo;
import org.farm.product.domain.bo.ProductSkuStockBo;
import org.farm.product.domain.bo.ProductSpuBo;
import org.farm.product.domain.convert.ProductSkuBoConvert;
import org.farm.product.domain.convert.ProductSkuStockBoConvert;
import org.farm.product.domain.convert.ProductSpuSkuAttributeValueBoConvert;
import org.farm.product.infra.basic.entity.ProductSku;
import org.farm.product.infra.basic.entity.ProductSkuStock;
import org.farm.product.infra.basic.entity.ProductSpuSkuAttributeValue;
import org.farm.product.infra.basic.service.ProductSkuService;
import org.farm.product.infra.basic.service.ProductSkuStockService;
import org.farm.product.infra.basic.service.ProductSpuSkuAttributeValueService;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Frank Zou
 * @version v0.0.1
 * @date 2025/4/30
 * @description
 */
@Slf4j
@Order(4)
@Component
public class ProductSpuSkuHandler implements ProductSpuHandler{


    private ProductSpuHandler nextHandler;

    private final ProductSkuService productSkuService;

    private final ProductSkuStockService productSkuStockService;

    private final ProductSpuSkuAttributeValueService productSpuSkuAttributeValueService;

    public ProductSpuSkuHandler(
            ProductSkuService productSkuService,
            ProductSkuStockService productSkuStockService,
            ProductSpuSkuAttributeValueService productSpuSkuAttributeValueService) {
        this.productSkuService = productSkuService;
        this.productSkuStockService = productSkuStockService;
        this.productSpuSkuAttributeValueService = productSpuSkuAttributeValueService;
    }

    @Override
    public void handler(ProductSpuBo productSpuBo) {
       List<ProductSkuBo> productSkuBoList = productSpuBo.getProductSkuList().stream().map(productSku -> {
            productSku.setId(IdWorker.getId());
            productSku.setSpuId(productSpuBo.getId());
            productSku.setStatus(ProductSkuStatusEnum.ENABLE.getValue());
            productSku.setCreatedAt(new Date());
            return productSku;
        }).toList();
        List<ProductSku> productSkuList = ProductSkuBoConvert.INSTANCE.convertBoListToEntityList(productSkuBoList);
        productSkuService.saveBatch(productSkuList);
        // 保存库存
        List<ProductSkuStock> productSkuStockList = productSkuBoList.stream().map(productSku -> {
            ProductSkuStockBo productSkuStock = productSku.getProductSkuStock();
            productSkuStock.setSkuId(productSku.getId());
            productSkuStock.setId(IdWorker.getId());
            productSkuStock.setCreatedAt(new Date());
            return ProductSkuStockBoConvert.INSTANCE.convertBoToEntity(productSkuStock);
        }).toList();

        if (!CollectionUtils.isEmpty(productSkuStockList)){
            productSkuStockService.saveBatch(productSkuStockList);
        }

        // 保存属性值
        List<ProductSpuSkuAttributeValue> attributeValueList = new ArrayList<>();
        productSkuBoList.forEach(productSkuBo -> {
            productSkuBo.getAttributeValues().forEach(attributeValue -> {
                attributeValue.setId(IdWorker.getId());
                attributeValue.setSpuId(productSpuBo.getId());
                attributeValue.setSkuId(productSkuBo.getId());
                attributeValue.setCreatedAt(new Date());
                attributeValueList.add(ProductSpuSkuAttributeValueBoConvert.INSTANCE.convertBoToEntity(attributeValue));
            });
        });
        if (!CollectionUtils.isEmpty(attributeValueList)){
            productSpuSkuAttributeValueService.saveBatch(attributeValueList);
        }

    }

    @Override
    public void setNextHandler(ProductSpuHandler nextHandler) {
        this.nextHandler = nextHandler;
    }

    @Override
    public ProductSpuHandler getNextHandler() {
        return this.nextHandler;
    }
}
